

//详情展开按钮
$('.item-list .ico-down').each(function(){
    $(this).click(function(){
        if($(this).hasClass('ico-up')){
            $(this).parents('.recommend').height('110px').end().removeClass('ico-up');
        }else{
            $(this).parents('.recommend').height('auto').end().addClass('ico-up');
        }
    })
})

//商品加减操作
$('.btn-ope-plus').click(function(){
    var cur_obj = $(this).parents('.ope-line')
    var cur_num_obj = cur_obj.find('.sp-ope-num');
    var t_num = parseInt(cur_num_obj.html())+1;
    cur_num_obj.html(t_num);
    if(t_num>0){
        cur_obj.find('.btn-ope-minus,.sp-ope-num').css('visibility','visible');
    }
})
$('.btn-ope-minus').click(function(){
    var cur_obj = $(this).parents('.ope-line')
    var cur_num_obj = cur_obj.find('.sp-ope-num');
    var t_num = parseInt(cur_num_obj.html());
    if(t_num==1){
        cur_obj.find('.btn-ope-minus,.sp-ope-num').css('visibility','hidden');
    }
    cur_num_obj.html(t_num-1);

})

function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x*100)/100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

//登陆
    $(".btnLogin").click(function(event) {
        if($("#myName").val()==""){
            $("#myName").next().css("display","inline-block");
            return false;
        }
        $("#myName").next().css("display","none");
        if($("#myTel").val()==""){
            $("#myTel").next().css("display","inline-block");
            return false;
        }
        $("#myTel").next().css("display","none");
        if(!isMobile($("#myTel").val())){
            $("#myTel").next().css("display","inline-block");
            return false;
        }
        $("#myTel").next().css("display","none");
        $.ajax({
            url: '/api/Interface.aspx?action=login',
            type: 'POST',
            dataType: 'json',
            data:{
                name:encodeURIComponent($("#myName").val()),
                tel:$("#myTel").val(),
            },
            success: function(data) {
                if(data.result==1){
                   $(".btnPer").addClass('btnPerOn');
                }else{
                    alert("用户不存在！");
                }
                $(".loginWrap").fadeOut(300);
                $('.bgMask').hide();
            }
        });

    });

    //获取省份
    function getProvince() {
        var str = "<option>请选择</option>";
        $.ajax({
            url: '/api/Interface.aspx?action=initpro',
            type: 'GET',
            dataType: 'json',
            success:function(data) {
                var _data = data.info;

                for(var i=0;i<_data.length;i++){
                    str += "<option proid="+_data[i].pid+">"+_data[i].province+"</option>";
                }

                $("#sltP").empty().append(str);
            }
        });
        
    }

    //getProvince();

    $("#sltP").change(function(event) {
        $(this).find('option:selected').val();
        var proid = $(this).find('option:selected').attr("proid");
        getCity(proid);
    });

    //获取城市
    function getCity(proid) {
        var str = "<option>请选择</option>";
        $.ajax({
            url: '/api/Interface.aspx?action=initcity&pid='+proid,
            type: 'GET',
            dataType: 'json',
            success:function(data) {
                var _data = data.info;

                for(var i=0;i<_data.length;i++){
                    str += "<option proid="+_data[i].cid+">"+_data[i].city+"</option>";
                }

                $("#sltC").empty().append(str);
            }
        });
        
    }

    //预约提交
    $(".btnReg").click(function(event) {
        var iswantVal = 0;

        if($("#myNameSub").val()==""){
            $("#myNameSub").next().css("display","inline-block");
            return false;
        }

        $("#myNameSub").next().css("display","none");

        if($("#myTelSub").val()==""){
            $("#myTelSub").next().css("display","inline-block");
            return false;
        }

        $("#myTelSub").next().css("display","none");

        if(!isMobile($("#myTelSub").val())){
            $("#myTelSub").next().css("display","inline-block");;
            return false;
        }  
        $("#myTelSub").next().css("display","none");     

        if($("#sltP option:selected").val()=="请选择"){
            $("#sltP").next().css("display","inline-block");
            return false;
        }
        $("#sltP").next().css("display","none");

        if($("#sltC option:selected").val()=="请选择"){
             $("#sltC").next().css("display","inline-block");
            return false;
        }

        $("#sltC").next().css("display","none");

        if($("#buyC option:selected").val()=="请选择"){
             $("#buyC").next().css("display","inline-block");
            return false;
        }

        $("#buyC").next().css("display","none");

        if($('input:radio:checked').val()=="是"){
            iswantVal = 1;
        }


        //提交ajax
        $.ajax({
            url: '/api/Interface.aspx?action=intestdriver',
            type: 'POST',
            dataType: 'json',
            data:{
                name:encodeURIComponent($("#myNameSub").val()),
                tel:$("#myTelSub").val(),
                province:encodeURIComponent($("#sltP option:selected").val()),
                city:encodeURIComponent($("#sltC option:selected").val()),
                buycardate:encodeURIComponent($("#buyC option:selected").val()),
                iswant:iswantVal,
                res:"wap"
            },
            success:function(data) {
                if(data.result==1){
                    $(".btnPer").addClass('btnPerOn');
                    alert("提交成功！");

                    //预约后直接提示中奖结果
                    //console.log(prizeFlag); 
                    if(prizeFlag){
                        //$('.btnMyLuck').trigger('click');
                        $('.btnMyLuck').click();
                        $('.btnToOrder').hide();
                    }
                }else if(data.result==100){
                    alert("已经提交过！");
                }else{
                    alert("提交失败！");
                }
                $(".regWrap").hide();
                $(".bgMask").hide();
            }
        });

    });


    //判断是否匹配手机
    function isMobile(val){
        if(val=="") {
            return false;
        }
        if (!val.match(/^1[3|4|5|7|8][0-9]\d{4,8}$/) || val.length != 11) {
            return false;
        } else {
            return true;
        }
    }
